package zero.tech.games.extension.queue;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import com.alibaba.fastjson.JSONObject;

import zero.tech.core.util.DatetimeUtil;
import zero.tech.games.common.InviteInfo;
import zero.tech.games.common.QueueConsts;
import zero.tech.games.common.ThreadRunner;
import zero.tech.games.entity.log.LogInviteLine;
import zero.tech.games.extension.runner.ServerInitProcess;
import zero.tech.games.extension.service.api.IExtensionService;
import zero.tech.redis.queue.QueueUtil;
@Configuration
@AutoConfigureAfter(ServerInitProcess.class)
public class InviteLineProcess extends ThreadRunner{
	private static final Logger logger=LoggerFactory.getLogger(InviteLineProcess.class);
	@Autowired
	private QueueUtil queueUtil;
	@Autowired
	private IExtensionService extensionService;
	@Override
	public void process() {
		while(true) {
			int sleep = 0;
			List<String> list = queueUtil.pullQueueBatch(QueueConsts.QUEUE_INVITE_LINE, 30);
			try {
				if (list == null || list.isEmpty()||!ServerInitProcess.ok) {
					sleep = 1000;
				} else {
					// 开始处理业务
					long time = System.currentTimeMillis();
					logger.info("============================开始邀请日志无限代计算==============================");
					
					List<InviteInfo> inviteInfos=new ArrayList<InviteInfo>();
					for(String str:list) {
						InviteInfo inviteInfo = JSONObject.parseObject(str,InviteInfo.class);
						inviteInfos.add(inviteInfo);
					}
					if(!inviteInfos.isEmpty()) {
						inviteHandle(inviteInfos);
					}
					logger.info("=====================结束邀请日志无限代计算,操作耗时:{}毫秒========================",
							(System.currentTimeMillis() - time));
				}
			} catch (Exception e) {
				e.printStackTrace();
				sleep = 1000;
			}
			if (sleep != 0) {
				try {
					Thread.sleep(sleep);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}		
	}
	private void inviteHandle(List<InviteInfo> inviteInfos) {
		List<LogInviteLine> logInviteLines=new ArrayList<>();
		for(InviteInfo info:inviteInfos) {
			String club_name=info.getClub_name();
			int club_id=info.getClub_id();
			int uid=info.getUid();
			String nick=info.getNick();
			String other_nick=info.getOther_nick();
			int other_uid=info.getOther_uid();
			Date time=info.getCreate_stamp();
			String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", time);
			List<Integer> fathers=extensionService.getParentIds(club_id, uid);
			
			//增加所有父级的邀请记录
			for(Integer father_uid:fathers) {
				LogInviteLine logInviteLine = new LogInviteLine();
				logInviteLine.setClub_id(club_id);
				logInviteLine.setUid(father_uid);
				logInviteLine.setInviter_uid(uid);
				logInviteLine.setInviter_nick(nick);
				logInviteLine.setOther_uid(other_uid);
				logInviteLine.setOther_nick(other_nick);
				logInviteLine.setClub_name(club_name);
				logInviteLine.setDay_str(day_str);
				logInviteLine.setCreate_stamp(time);
				logInviteLines.add(logInviteLine);
			}
			LogInviteLine logInviteLine = new LogInviteLine();
			logInviteLine.setClub_id(club_id);
			logInviteLine.setUid(other_uid);
			logInviteLine.setInviter_uid(uid);
			logInviteLine.setInviter_nick(nick);
			logInviteLine.setOther_uid(other_uid);
			logInviteLine.setOther_nick(other_nick);
			logInviteLine.setClub_name(club_name);
			logInviteLine.setDay_str(day_str);
			logInviteLine.setCreate_stamp(time);
			logInviteLines.add(logInviteLine);
			
			LogInviteLine logInviteLine2 = new LogInviteLine();
			logInviteLine2.setClub_id(club_id);
			logInviteLine2.setUid(uid);
			logInviteLine2.setInviter_uid(uid);
			logInviteLine2.setInviter_nick(nick);
			logInviteLine2.setOther_uid(other_uid);
			logInviteLine2.setOther_nick(other_nick);
			logInviteLine2.setClub_name(club_name);
			logInviteLine2.setDay_str(day_str);
			logInviteLine2.setCreate_stamp(time);
			logInviteLines.add(logInviteLine2);
		}
		if(!logInviteLines.isEmpty()) {
			queueUtil.putQueueBatch(QueueConsts.QUEUE_LOG_INVITE_LINE, logInviteLines);
		}
	}
}
